package com.loongtech.bi.advertisment.controller;

import com.loongtech.bi.advertisment.model.AdvWarningData;
import com.loongtech.bi.advertisment.service.PayUserService;
import com.loongtech.bi.support.Session;
import com.loongtech.core.util.Time;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 
 * ClassName: AdvWarnDataAction
 * 
 * @Description 广告异常数据
 * @author liuchen
 * @date 2017-7-29 下午2:35:53
 */
@Controller("advWarnDataAction")
@Scope("view")
public class AdvWarnDataAction extends BaseAdvAction {

	private String dateRadio = "0"; // 切换的天数：1=昨天。7=近7天。15=近15天。30=近30天，0=自选时间

	private Date dateStart = new Date(Time.getBeginningOfDay(Time.currentTime() - 0)); // 自选时间：开始时间
	private Date dateEnd = new Date(Time.getBeginningOfDay(Time.currentTime() - 0)); // 自选时间：结束时间

	private List<AdvWarningData> warningDatas0;
	private List<AdvWarningData> warningDatas1;
	private List<AdvWarningData> warningDatas2;
	private List<AdvWarningData> warningDatas3;

	private static final long serialVersionUID = 1L;

	@Resource(name = "payUserService")
	private PayUserService payUserService;

	@Resource
	Session session;

	@PostConstruct
	public void init() {
		search();
	}

	public void search() {
		FacesContext context = FacesContext.getCurrentInstance();
		if (hasAgents == null || hasAgents.size() <= 0) {
			context.addMessage(null, new FacesMessage("提示", "此用户无任何代理商权限，请联系管理员配置!"));
			return;
		}
		if ((selectedAgent == null || selectedAgent.length <= 0) && !"admin".equals(session.getUserName())) {
			context.addMessage(null, new FacesMessage("提示", "请选择代理商!"));
			return;
		}

		warningDatas0 = new ArrayList<AdvWarningData>();
		warningDatas1 = new ArrayList<AdvWarningData>();
		warningDatas2 = new ArrayList<AdvWarningData>();
		warningDatas3 = new ArrayList<AdvWarningData>();
		try {
			String startDay = Time.getDateStr(new Date(Time.getBeginningOfDay(("-1".equals(dateRadio) ? dateStart
					: new Date(Time.getBeginningOfDay(Time.currentTime() - (Integer.parseInt(dateRadio)) * Time.DAY)))
					.getTime()))).replaceAll("-", ""); // 开始时间
			String endDay = Time.getDateStr(new Date(Time.getBeginningOfDay(("-1".equals(dateRadio) ? dateEnd
					: new Date(Time.getBeginningOfDay(Time.currentTime()))).getTime()))).replaceAll("-", ""); // 结束时间

			warningDatas0 = payUserService.getListWarningData(startDay, endDay, selectedChannelName, selectedAgent,
					selectedPlatform, "-1");
			warningDatas1 = payUserService.getListWarningData(startDay, endDay, selectedChannelName, selectedAgent,
					selectedPlatform, "1");
			warningDatas2 = payUserService.getListWarningData(startDay, endDay, selectedChannelName, selectedAgent,
					selectedPlatform, "2");
			warningDatas3 = payUserService.getListWarningData(startDay, endDay, selectedChannelName, selectedAgent,
					selectedPlatform, "3");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String getDateRadio() {
		return dateRadio;
	}

	public void setDateRadio(String dateRadio) {
		this.dateRadio = dateRadio;
	}

	public Date getDateStart() {
		return dateStart;
	}

	public void setDateStart(Date dateStart) {
		this.dateStart = dateStart;
	}

	public Date getDateEnd() {
		return dateEnd;
	}

	public void setDateEnd(Date dateEnd) {
		this.dateEnd = dateEnd;
	}

	public List<AdvWarningData> getWarningDatas0() {
		return warningDatas0;
	}

	public void setWarningDatas0(List<AdvWarningData> warningDatas0) {
		this.warningDatas0 = warningDatas0;
	}

	public List<AdvWarningData> getWarningDatas1() {
		return warningDatas1;
	}

	public void setWarningDatas1(List<AdvWarningData> warningDatas1) {
		this.warningDatas1 = warningDatas1;
	}

	public List<AdvWarningData> getWarningDatas2() {
		return warningDatas2;
	}

	public void setWarningDatas2(List<AdvWarningData> warningDatas2) {
		this.warningDatas2 = warningDatas2;
	}

	public List<AdvWarningData> getWarningDatas3() {
		return warningDatas3;
	}

	public void setWarningDatas3(List<AdvWarningData> warningDatas3) {
		this.warningDatas3 = warningDatas3;
	}

}
